from collections.abc import Sequence
import pickle


def brief(sequence: Sequence, ends_len: int = 3) -> str:
    """
    Return the brief content of a sequence, with elements in the middle part omitted.

    :param sequence: input sequence
    :param ends_len: a integer representing the number of elements to keep at the head and tail
    :return: a str representing the brief content of the sequence
    """

    # if the sequence is too short
    if len(sequence) <= ends_len * 2:
        return str(sequence)

    # if the sequence is a range instance, convert it to str and return since it's inherently brief
    if isinstance(sequence, range):
        return str(sequence)

    # if the sequence is a str
    if isinstance(sequence, str):
        return sequence[:ends_len] + ' ... ' + sequence[-ends_len:]

    # otherwise, e.g. a list, a tuple
    head, tail = str(sequence[:ends_len]), str(sequence[-ends_len:])
    if head.endswith((')', ']', '}')):
        head = head[:-1]
    if tail.startswith(('(', '[', '{')):
        tail = tail[1:]
    return head + ', ..., ' + tail


def save_variable(v,filename):
    f = open(filename, 'wb')
    pickle.dump(v, f)
    f.close()
    return filename


def load_variable(filename):
    f = open(filename, 'rb')
    r = pickle.load(f)
    f.close()
    return r


if __name__ == '__main__':
    print(brief([[1, 2], [1, 2], [1, 2], [1, 2], [1, 2], [1, 2], [1, 2], [1, 2], [1, 2]], 3))
    print(brief('123456789101112131415', 8))
    print(brief(range(100)))
    print(brief({1: 1, 2: 3, 4: 5, 6: 7, 8: 9, 10: 11, 12: 13, 14: 15}))  # fail: dict is not a Sequence
